package com.top.mall.main.order

import android.app.Activity
import android.content.Intent
import android.os.Bundle
import android.view.View
import com.blankj.utilcode.util.ActivityUtils
import com.blankj.utilcode.util.BarUtils
import com.module.lemlin.owner.OwnerViewModeActivity
import com.top.mall.R
import com.top.mall.entity.OrderDetails
import com.top.mall.jetpack.vm.OrderViewModel
import com.top.mall.main.order.adapter.OrderGoodsAdapter
import kotlinx.android.synthetic.main.activity_order_details.*
import kotlinx.android.synthetic.main.activity_order_details.imageViewBack
import kotlinx.android.synthetic.main.include_order_address.*
import kotlinx.android.synthetic.main.include_order_attribute.*
import kotlinx.android.synthetic.main.include_order_expense2.*
import kotlinx.android.synthetic.main.include_order_goods.*
import kotlinx.android.synthetic.main.include_order_status.*

class OrderDetailsActivity : OwnerViewModeActivity<OrderViewModel>() {

    companion object {
        private const val KEY_ORDER_DETAILS = "KEY_ORDER_DETAILS"
        fun open(orderDetails: OrderDetails) {
            val bundle = Bundle().apply {
                putSerializable(KEY_ORDER_DETAILS, orderDetails)
            }
            ActivityUtils.startActivity(bundle, OrderDetailsActivity::class.java)
        }
    }

    private val mOrderDetails: OrderDetails?
        get() = intent?.extras?.getSerializable(KEY_ORDER_DETAILS) as? OrderDetails

    override fun viewModel(): OrderViewModel {
        return createViewModel()
    }

    override val layoutResId: Int
        get() = R.layout.activity_order_details

    override fun stateBar(): StatusBar {
        return StatusBar(view = flOrderDetailsBar)
    }

    override fun initView() {

        imageViewBack.setOnClickListener { finish() }
        BarUtils.setStatusBarColor(this, resources.getColor(R.color.colorMain))
        BarUtils.setStatusBarLightMode(this, false)

        mOrderDetails?.let { fillData(it) }
    }

    override fun initData() {

    }

    private fun fillData(orderDetails: OrderDetails) {

        if (orderDetails.pay_status == "0") {
            tvOrderStatusPay.visibility = View.VISIBLE
            tvOrderStatusQuxiao.visibility = View.VISIBLE
            tvOrderStatusZailai.visibility = View.GONE
            tvOrderStatusTis.text = "待支付"
        } else {
            tvOrderStatusPay.visibility = View.GONE
            tvOrderStatusQuxiao.visibility = View.GONE
            tvOrderStatusZailai.visibility = View.VISIBLE
            tvOrderStatusTis.text = "待发货"
        }

        rvOrderGoodsBody.adapter =
            OrderGoodsAdapter().apply {
                setNewInstance(orderDetails.order_goods)
            }


        tvOrderExpenseDel1.text = orderDetails.goods_price
        tvOrderExpenseDel2.text = orderDetails.coupon_price
        tvOrderExpenseDel3.text = orderDetails.total_amount
        tvOrderExpenseDel4.text = orderDetails.shipping_price
        tvOrderExpenseDel5.text = orderDetails.cash_coupon_price
        tvOrderExpenseDel6.text = orderDetails.order_amount

        tvOrderAddress1.text = orderDetails.delivery_time
        tvOrderAddress2.text = "${orderDetails.consignee}\t${orderDetails.mobile}"
        tvOrderAddress3.text = orderDetails.shops.areaAddress

        tvOrderAttribute1.text = orderDetails.order_sn
        tvOrderAttribute2.text = orderDetails.createtime
        tvOrderAttribute3.text = if (orderDetails.pay_status == "1") "user_money" else ""
        tvOrderAttribute4.text = orderDetails.order_sn
    }
}